/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package smaragJava;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * 
 * @author northpole
 */
public class Connector {

	private static Connection con;

	public Connector() {
		
		  try { 
			  Class.forName("com.mysql.jdbc.Driver");
			  con = DriverManager.getConnection("jdbc:mysql://localhost:3306/texlog","northpole", "northy");
		  } catch (SQLException ex) {
			  Logger.getLogger(Connector.class.getName()).log(Level.SEVERE, null, ex); 
		  } catch (ClassNotFoundException ex) {
			  Logger.getLogger(Connector.class.getName()).log(Level.SEVERE, null, ex); 
		  }
		  
	}

	public static Connection getConnection() {
		return con;
	}

        public static String abastractExecute(String []fields,PreparedStatement stmt){
        try {
            ArrayList result = new ArrayList();
            ResultSet rs;
            rs = stmt.executeQuery();
            while (rs.next()) {
                for (int i = 0; i < fields.length; i++) {
                    result.add(rs.getString(fields[i]));
                }
            }
            return null;

        } catch (SQLException ex) {
            Logger.getLogger(Connector.class.getName()).log(Level.SEVERE, null, ex);
        }
        throw new UnsupportedOperationException("Not yet implemented");}

	public static String abstractSelect(String[] fields, String[] data, String by, String table) {

		System.out.println(data.length);

		ArrayList keys = new ArrayList();
		String query = "SELECT ";
		String queryf = " FROM " + table;
		String queryw = " WHERE ";
		if (fields.length < 1) {
			return "ERROR:NO FIELDS";
		}
		if (data.length < 1) {
			return "ERROR:NO DATA";
		}
		for (int i = 0; i < fields.length - 1; i++) {
			query += fields[i] + ", ";
		}
		//query += fields[fields.length - 1];

		query += queryf;
		query += queryw;

		for (int i = 0; i < data.length - 1; i++) {

			query += by + "=? OR ";
		}
		query += by + "=?;";
		System.out.println(query);

		return query;
	}
	
	public static String abstractSelect(String table){
		String query = "SELECT * FROM "+table;
		return query;
	}
	
	public static String abstractInsert(String  table, String[] values){
		String query = "INSERT INTO ";
		query+= table+=" VALUES (";
		
		for ( int i=0; i < values.length-1; i++){
			query += values[i]+", ";
		}
		query+= ")";
		return query;
	}
	
	public static String abstractUpdate(String[] data, int pk, String table){
		
		/*2 DO
		 * 
		 * ArrayList keys = new ArrayList();
		String query = "UPDATE "+table+"SET ";		
		String queryw = " WHERE ";
		if (fields.length < 1) {
			return "ERROR:NO FIELDS";
		}
		if (data.length < 1) {
			return "ERROR:NO DATA";
		}
		for (int i = 0; i < data.length - 1; i++) {
			query += data[i] + ", ";
		}
		//query += fields[fields.length - 1];
		
		query += queryw;

		for (int i = 0; i < data.length - 1; i++) {

			query += by + "=? OR ";
		}
		query += by + "=?;";
		System.out.println(query);*/

		return null;
	}

	public static String abstractDelete(String[] fields, String[] data, String by, String table){
		
		System.out.println(data.length);

		ArrayList keys = new ArrayList();
		String query = "DELETE ";
		String queryf = " FROM " + table;
		String queryw = " WHERE ";
		if (fields.length < 1) {
			return "ERROR:NO FIELDS";
		}
		if (data.length < 1) {
			return "ERROR:NO DATA";
		}
		for (int i = 0; i < fields.length - 1; i++) {
			query += fields[i] + " = " + data[i];
		}
		//query += fields[fields.length - 1];

		query += queryf;
		query += queryw;

		for (int i = 0; i < data.length - 1; i++) {

			query += by + "=? OR ";
		}
		query += by + "=?;";
		System.out.println(query);

		return query;
		
	}
	
	
	
	/*
	 * public static String abstractSelect(String field,String data,String
	 * by,String table){
	 * 
	 * // System.out.println(data.length);
	 * 
	 * 
	 * 
	 * String queryf=" FROM "+table; String queryw=" WHERE "; if (field==null) {
	 * return "ERROR:NO FIELDS"; } if (data==null) { return "ERROR:NO DATA"; }
	 * String query="Select "+field+queryf+queryw+by+" = ?";
	 * 
	 * System.out.println(query);
	 * 
	 * return query; }
	 */

	protected void finalize() {

		if (con != null) {
			try {
				con.close();
			} catch (SQLException ex) {
				Logger.getLogger(Connector.class.getName()).log(Level.SEVERE,
						null, ex);
			}
		}

	}

}
